// userLib.h

#include <p18cxxx.h>
#include <usart.h>
#include <i2c.h>
#include <stdio.h>

// common data types as applied to PIC18

typedef unsigned int uint16;
typedef int int16;
typedef unsigned long uint32;
typedef long int32;

// NOTE Timer Osc Freq = Fosc /4 BEFORE prescaling

#define TIMER_PRESCALE 1
#define DELAY_OVERHEAD_TICKS 100 

// Clock and timer settings

extern uint32 sysclk;			// system clock speed
extern uint32 pbclk;			// peripheral bus clock speed
extern uint16 timer_ticks_per_microsec;  // time base for delays; See userLib.h for prescale
extern unsigned char freeclk_timer_reset_value; // free running clock setting made at reset or at interrupt	 

// defines 

// stdio functions

#define getc ReadUSART
#define putc WriteUSART

// set TxCON reg for osc enable and timer on, prescalars value for 1:1
// note that these values are different depending on timer

#define T0CON_VAL	0x88;	// Timer osc enable and on, Prescale 1:1
#define T1CON_VAL	0xC9;	// Timer osc enable and on, Prescale 1:1
#define T2CON_VAL	0x4;	// Timer osc enable and on, Prescale 1:16
#define T3CON_VAL	0xC9;	// Timer osc enable and on, Prescale 1:1
// Note T4 uses timer 0
//#define T23_PRESCALER T2_PS_1_8
//#define T45_PRESCALER T4_PS_1_8

#define PRINTF_BAUDRATE	230400				// baud rate for printf output

// userLib.c function prototypes

extern uint32 picInit( uint32 primaryClockSpeed, uint32 periphBusClockSpeed );

// timer functions using numbered timers

extern void delayT0_init(void);
extern void delayT0_us( uint16 us );
extern void delayT0_ms( uint16 ms );
extern void delayT1_init(void);
extern void delayT1_us( uint16 us );
extern void delayT1_ms( uint16 ms );
extern void delayT2_init(void);
extern void delayT2_us( uint16 us );
extern void delayT2_ms( uint16 ms );
extern void delayT3_init(void);
extern void delayT3_us( uint16 us );
extern void delayT3_ms( uint16 ms );

// freeclk functions

extern void freeclk_init(void);
extern uint32 freeclk_read(void);
extern void freeclk_interrupt(void);

// i2c functions

extern int writeBytesI2C1( unsigned char *buf, 	
					 	   unsigned char len );	
extern int maxim517DA1( unsigned char port, 
           	    		unsigned char val );

void A2D_init(char maxA2Dchan);
int A2D_read(unsigned char chan);

